package com.linxz.ui.dialogs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.linxz.ui.R;
import com.linxz.ui.listener.QRSaveListener;
import com.linxz.ui.listener.UserInfoActions;
import com.linxz.ui.listener.WebSaveListener;

/**
 * @author Linxz
 * 创建日期：2019年05月17日 10:26
 * version：v4.0.1
 * 描述：
 */
public class DialogHelper {

    private String mTittle;
    private String mMessage;
    private String mLeftBtnWords;
    private String mRightBtnWords;
    private OnCancleListener mOnCancelListener;
    private OnConfirmListener mOnConfirmListener;
    private OnConfirmWithResult onConfirmWithResult;

    public static Builder builder() {
        return new Builder();
    }

    private DialogHelper(String title,
                         String message,
                         String leftWords,
                         String rightWords,
                         OnCancleListener onCancleListener,
                         OnConfirmListener onConfrimListener,
                         OnConfirmWithResult onConfirmWithResult) {
        this.mTittle = title;
        this.mMessage = message;
        this.mLeftBtnWords = leftWords;
        this.mRightBtnWords = rightWords;
        this.mOnCancelListener = onCancleListener;
        this.mOnConfirmListener = onConfrimListener;
        this.onConfirmWithResult = onConfirmWithResult;
    }

    public interface OnCancleListener {
        void onCancleClick();
    }

    public interface OnConfirmListener {
        void onConfirmClick();
    }

    public interface OnConfirmWithResult {
        void onConfirmClick(String arg1, String arg2);
    }

    public static class Builder {
        private String mTittle = "";
        private String mMessage;
        private String mLeftBtnWords="取消";
        private String mRightBtnWords="确定";
        private OnCancleListener mOnCancleListener;
        private OnConfirmListener mOnConfirmListener;
        private OnConfirmWithResult mOnConfirmWithResult;

        public Builder tittle(String tittle) {
            mTittle = tittle;
            return this;
        }

        public Builder message(String message) {
            this.mMessage = message;
            return this;
        }

        public Builder leftWords(String leftWords) {
            this.mLeftBtnWords = leftWords;
            return this;
        }

        public Builder rightWords(String rightWords) {
            this.mRightBtnWords = rightWords;
            return this;
        }

        public Builder cancleListener(OnCancleListener onCancleListener) {
            this.mOnCancleListener = onCancleListener;
            return this;
        }

        public Builder confirmListener(OnConfirmListener onConfirmListener) {
            this.mOnConfirmListener = onConfirmListener;
            return this;
        }

        public Builder confirmListener(OnConfirmWithResult onConfirmListener) {
            this.mOnConfirmWithResult = onConfirmListener;
            return this;
        }

        public DialogHelper build() {
            return new DialogHelper(mTittle, mMessage, mLeftBtnWords, mRightBtnWords, mOnCancleListener, mOnConfirmListener, mOnConfirmWithResult);
        }

    }

    /**
     * 更新基础题库
     */
    public AlertDialog showBaseDialog(Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(mTittle);
        builder.setCancelable(false);
        builder.setMessage(mMessage);
        if (mOnCancelListener != null) {
            builder.setNegativeButton(mLeftBtnWords, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mOnCancelListener.onCancleClick();
                }
            });

        }
        if (mOnConfirmListener != null) {
            builder.setPositiveButton(mRightBtnWords, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mOnConfirmListener.onConfirmClick();
                }
            });
        }
        final AlertDialog mDialog = builder.create();
        mDialog.show();
        return mDialog;
    }


    /**
     * 发布动态，添加链接
     */
    public Dialog showLinkDialog(final Context context) {
        final Dialog dialog = new Dialog(context, R.style.promptDialog);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.ui_dialog_link_insert, null);
        TextView tvTittle = view.findViewById(R.id.tvTittle);
        final EditText edtLink = view.findViewById(R.id.edtLink);
        final EditText edtDesc = view.findViewById(R.id.edtDesc);
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        tvTittle.setText(mTittle);
        tvCancel.setText(mLeftBtnWords);
        tvConfirm.setText(mRightBtnWords);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edtLink.getText().toString().trim())) {
                    Toast.makeText(context, "请输入链接", Toast.LENGTH_SHORT).show();
                } else {
                    if (onConfirmWithResult != null) {
                        onConfirmWithResult.onConfirmClick(edtLink.getText().toString().trim(), edtDesc.getText().toString().trim());
                    }
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
        return dialog;
    }


    public void showGenderDialog(Context context) {
        final Dialog genderDialog = new Dialog(context, R.style.ui_default_dialog_style);
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.ui_dialog_set_sex, null);
        genderDialog.setContentView(view);
        Window window = genderDialog.getWindow();
        // 添加动画
        window.setWindowAnimations(R.style.ui_dialog_mystyle);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        genderDialog.show();
        genderDialog.setCanceledOnTouchOutside(true);
        Button man = view.findViewById(R.id.man_rl);
        Button woman = view.findViewById(R.id.woman_rl);
        Button secrecy = view.findViewById(R.id.rl_secrecy);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = v.getId();
                if (i == R.id.man_rl) {
                    genderDialog.cancel();
                    if (onConfirmWithResult != null) {
                        onConfirmWithResult.onConfirmClick("0", "男");
                    }
                } else if (i == R.id.woman_rl) {
                    if (onConfirmWithResult != null) {
                        onConfirmWithResult.onConfirmClick("1", "女");
                    }
                }
                genderDialog.cancel();
            }
        };
        man.setOnClickListener(listener);
        woman.setOnClickListener(listener);
        secrecy.setOnClickListener(listener);
    }


    public void showUserInfoControllerDialog(Context context, final UserInfoActions outListener){
        final Dialog dialog = new Dialog(context, R.style.ui_default_dialog_style);
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.ui_dialog_layout_userinfo, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        // 添加动画
        if (window!=null){
            window.setWindowAnimations(R.style.ui_dialog_mystyle);
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
        TextView tvShareWx=view.findViewById(R.id.tvShareWx);
        TextView tvShareWxFriends=view.findViewById(R.id.tvShareWxFriends);
        TextView tvShareQQ=view.findViewById(R.id.tvShareQQ);
        TextView tvShareQQZone=view.findViewById(R.id.tvShareQQZone);
        TextView tvShareApp=view.findViewById(R.id.tvShareApp);
        TextView tvReport=view.findViewById(R.id.tvReport);
        TextView tvPullBack=view.findViewById(R.id.tvPullBack);
        TextView tvCancel=view.findViewById(R.id.tvCancel);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId()== R.id.tvShareWx){
                    outListener.shareWx();
                }else if (v.getId()== R.id.tvShareWxFriends){
                    outListener.shareWxFriends();

                }else if (v.getId()== R.id.tvShareQQ){
                    outListener.shareQQ();
                }else if (v.getId()== R.id.tvShareQQZone){
                    outListener.shareQQZone();
                }else if (v.getId()== R.id.tvShareApp){
                    outListener.shareApp();
                }else if (v.getId()== R.id.tvReport){
                    outListener.report();
                }else if (v.getId()== R.id.tvPullBack){
                    outListener.pullBack();
                }
                dialog.dismiss();
            }
        };
        tvShareWx.setOnClickListener(listener);
        tvShareWxFriends.setOnClickListener(listener);
        tvShareQQ.setOnClickListener(listener);
        tvShareQQZone.setOnClickListener(listener);
        tvShareApp.setOnClickListener(listener);
        tvReport.setOnClickListener(listener);
        tvPullBack.setOnClickListener(listener);
        tvCancel.setOnClickListener(listener);
    }



    public void showDynamicPullBack(Context context, final View.OnClickListener onClickListener){
        final Dialog dialog = new Dialog(context, R.style.ui_default_dialog_style);
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.ui_dialog_layout_dynamic_list, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        // 添加动画
        if (window!=null){
            window.setWindowAnimations(R.style.ui_dialog_mystyle);
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
        view.findViewById(R.id.tvPullBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onClick(v);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    public void showQRcodeSave(Context context, final QRSaveListener qrSaveListener){
        final Dialog dialog = new Dialog(context, R.style.ui_default_dialog_style);
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.ui_dialog_layout_home_qrcode_save, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        // 添加动画
        if (window!=null){
            window.setWindowAnimations(R.style.ui_dialog_mystyle);
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
        view.findViewById(R.id.tvShare).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qrSaveListener.onShareClick();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvSave).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qrSaveListener.onSaveClick();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    public void showJokeShare(Context context, final QRSaveListener qrSaveListener){
        final Dialog dialog = new Dialog(context, R.style.ui_default_dialog_style);
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.ui_dialog_layout_home_qrcode_save, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        // 添加动画
        if (window!=null){
            window.setWindowAnimations(R.style.ui_dialog_mystyle);
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
        TextView tvShare=view.findViewById(R.id.tvShare);
        TextView tvCopy=view.findViewById(R.id.tvSave);
        tvCopy.setText("复制");
        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qrSaveListener.onShareClick();
                dialog.dismiss();
            }
        });
        tvCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qrSaveListener.onSaveClick();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    public void webViewShare(Context context, final WebSaveListener webSaveListener){
        final Dialog dialog = new Dialog(context, R.style.ui_default_dialog_style);
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.ui_dialog_layout_home_web_save, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        // 添加动画
        if (window!=null){
            window.setWindowAnimations(R.style.ui_dialog_mystyle);
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        view.findViewById(R.id.tvShare).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webSaveListener.onWxFriends();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvShareWxFriendsCircle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webSaveListener.onWxCircle();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvCollect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webSaveListener.onCollect();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tvSave).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webSaveListener.onCopy();
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);

    }

}
